package com.training1.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.training1.core.common.global.Global;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.security.MD5;
import com.training1.core.dto.APIResultDto;
import com.training1.core.entity.MemberEntity;
import com.training1.core.entity.SystemParamEntity;
import com.training1.core.modules.dao.SystemSettingsDao;
import com.training1.core.modules.service.MemberService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "/api/login", description = "演示如何使用API文档") // Swagger annotation
@RestController
@RequestMapping("/api/login")
public class APILoginController {
	private static final Logger logger = LoggerFactory.getLogger(APILoginController.class);
	
	@Resource
	private MemberService memberService;
	@Resource
	private SystemSettingsDao systemSettingDao;
	
	@ApiOperation(value = "api会员登录, 第一个参数是会员手机号码，第二个参数是会员密码")
	@RequestMapping(value = "/applogin", method = RequestMethod.GET)
	public APIResultDto<Object> login( @RequestParam String cellPhone,@RequestParam String password) {
		logger.info("客户端会员登录, cellphone : " + cellPhone);
		APIResultDto<Object> dto = new APIResultDto<Object>();
		//判断是否为系统运行时间
		//抽取系统开始时间参数
		SystemParamEntity begintime = systemSettingDao.findEntityByKey("SYSTEM_RUN_BEGIN");
		SystemParamEntity endtime = systemSettingDao.findEntityByKey("SYSTEM_RUN_END");
		String nowdate = DateUtils.getTime();
		if(nowdate.compareTo(begintime.getParamValue()) > 0 && nowdate.compareTo(endtime.getParamValue()) < 0) {
		} else {
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("3");
			dto.setMessage(Global.getMessage("api.error.login.systemProtect"));
			return dto;
		}
		MemberEntity member = memberService.findMember(cellPhone);
		if(member == null){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("2");
			dto.setMessage(Global.getMessage("api.error.login.usernameOrpasswordError"));
			return dto;
		}
		String passWord = MD5.getMD5(password);
		if(member.getPassword().equals(passWord)){
			Integer flg = member.getBlackListFlag();
			if(flg.equals(0)){
				String token = memberService.successLogin(member);
				//登陆成功
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("token", token);
				dto.setData(param);
				dto.setStatus(APIResultDto.SUCCESS_OK);
				dto.setErrorCode("1");
				dto.setMessage(Global.getMessage("api.error.login.success"));
				return dto;
			}else{
				dto.setStatus(APIResultDto.ERROR);
				dto.setErrorCode("3");
				dto.setMessage(Global.getMessage("api.error.login.memberLock"));
				return dto;
			}
		}else{
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("2");
			dto.setMessage(Global.getMessage("api.error.login.usernameOrpasswordError"));
			return dto;
		}
	}
}